package com.yaozheng.vocationaltraining.service.impl.exam;

import com.yaozheng.vocationaltraining.iview.BaseView;
import com.yaozheng.vocationaltraining.iview.IBaseView;
import com.yaozheng.vocationaltraining.iview.exam.IExamHelpView;
import com.yaozheng.vocationaltraining.service.exam.ExamHelpService;
import com.yaozheng.vocationaltraining.utils.Constants;
import com.yaozheng.vocationaltraining.utils.ErrorUtils;
import com.yaozheng.vocationaltraining.utils.http.okhttp.OkHttpClientManager;

import net.sf.json.JSONObject;

import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.EBean;

/**
 * 道具卡
 */
@EBean
public class ExamHelpServiceImpl implements ExamHelpService {
    IExamHelpView iExamHelpView;

    @Override
    public void init(IExamHelpView iExamHelpView) {
        this.iExamHelpView = iExamHelpView;
    }

    @Background
    @Override
    public void getPropsCard(int examId, int itemId, int questionId) {
        try {
            OkHttpClientManager.getInstance().get(
                    Constants.ROOTURL + "/exam/" + examId + "/item/" + itemId + "?questionId="
                            + (itemId == 2 ? questionId : ""),
                    iExamHelpView.getToken(), new BaseView(iExamHelpView) {
                        @Override
                        public void responseSuccess(JSONObject jsonObject) {
                            iExamHelpView.propsCardSuccess(jsonObject);
                        }

                        @Override
                        public void responseError(String error) {
                            iExamHelpView.propsCardError(error);
                        }

                    });
        } catch (Exception e) {
            ErrorUtils.outErrorLog(e);
            if (iExamHelpView.isResponseResult()) {
                iExamHelpView
                        .propsCardError(ErrorUtils.SERVER_CONNECTION_ERROR);
            }
        }
    }
}
